﻿using System;
using System.ServiceModel.Syndication;
using System.ServiceModel.Web;
using System.Web;
using System.Xml;
using ScottLane.MediaFeeder.ObjectModel;

namespace ScottLane.MediaFeeder.WebService
{
    /// <summary>
    /// A SyndicationItem representing a Playlist.
    /// </summary>
    public class PlaylistSyndicationItem : SyndicationItem
    {
        private Playlist playlist;

        public PlaylistSyndicationItem(Playlist playlist)
        {
            this.playlist = playlist;
            this.Title = new TextSyndicationContent(playlist.Name);
            string playlistUrl = Utility.BaseUrl + "/music/playlists/" + HttpUtility.UrlEncode(playlist.Name);
            this.Id = playlistUrl;
            this.Links.Add(SyndicationLink.CreateAlternateLink(new Uri(playlistUrl)));
        }

        protected override void WriteElementExtensions(XmlWriter writer, string version)
        {
            writer.WriteElementString("mf:type", playlist.Type.ToString());
            writer.WriteElementString("mf:songCount", playlist.Songs.Count.ToString());
            writer.WriteElementString("mf:duration", playlist.Duration.ToString());
        }
    }
}
